package com.atguigu.juc.chap10;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureDemo07 {

    public static void main(String[] args) {

        CompletableFuture.supplyAsync(() -> {
                    System.out.println("第一个任务");
                    return "first task result";
                }).thenApply((String prevTaskResult) -> {
                    System.out.println("第二个任务 " + (10 / 0));
                    return prevTaskResult;
                })
                // 针对前面所有任务提供异常处理的功能
                // 需要传入的对象：Function<Throwable, ? extends T> fn 类型
                .exceptionally((Throwable t) -> {

                    String exceptionFullName = t.getClass().getName();
                    System.out.println("exceptionFullName = " + exceptionFullName);

                    Throwable cause = t.getCause();
                    System.out.println("cause.getClass().getName() = " + cause.getClass().getName());

                    String message = t.getMessage();
                    System.out.println("message = " + message);

                    return "前面任务抛出了异常，由 exceptionally() 方法提供一个备选任务结果。";
                }).whenComplete((String finalResult, Throwable throwable) -> {
                    System.out.println("finalResult = " + finalResult);
                    System.out.println("throwable = " + throwable);
                });

        System.out.println("finished");
    }
}